package com.xt.service.impl;

import com.xt.bean.Article;
import com.xt.dao.ArticleRepository;
import com.xt.dto.ArticleListSearchDto;
import com.xt.service.ArticleService;
import com.xt.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by XT on 2018/5/4.
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository ar;

    @Override
    public void saveOrUpdateOne(Article a){

        if (StringUtils.isEmpty(a.getId())){
            //新增
            a.setCreateDate(new Date());
        }

        a.setUpdateDate(new Date());
        ar.save(a);
    }

    @Override
    public void deleteOne(String id){
        //删除关联关系

        ar.delete(id);
    }

    @Override
    public Article getById(String id){
        return ar.findOne(id);
    }

    @Override
    public Page<Article> getPage(ArticleListSearchDto dto) {
        Page<Article> articlesPage = ar.findByTitleLikeAndAuthorIdLikeAndContentLikeAndTagLikeAndAtypeLikeAndKnowledgePointIdLike(
                dto.getTitle(),
                dto.getAuthorId(),
                dto.getContent(),
                dto.getTag(),
                dto.getAtype(),
                dto.getKnowledgePointId(),
                PageUtil.getPageable(dto.getCount(),dto.getPageNo(),dto.getSortDirection(),dto.getSortField()));
        return articlesPage;
    }



}

